package com.beyondsoft.dtc.mobile.automation.redis;

/**
 * Created by Administrator on 2/23/2017.
 */
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beyondsoft.dtc.mobile.automation.impl.compatibility.CompatibilityTestExecutor;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisClient {
    private JedisPool jedisPool;//非切片连接池
    private ShardedJedisPool shardedJedisPool;//切片连接池
    private static RedisClient redisClient;
    
    private static final Logger logger = LoggerFactory
			.getLogger(CompatibilityTestExecutor.class);
    
    private RedisClient() {    	
        initialPool();       
        initialShardedPool();
    }

    public static RedisClient getRedisClient(){
    	logger.info("getRedisClient()");
        if(redisClient==null){
            synchronized (RedisClient.class) {
                if(redisClient==null) {
                	 logger.info("new RedisClient()");
                	redisClient = new RedisClient();
                }
            }
        }
        return redisClient;
    }

    public Jedis getJedis() {
        return jedisPool.getResource();
    }

    public ShardedJedis getShardedJedis() {
        return  shardedJedisPool.getResource();
    }

    /**
     * 初始化非切片池
     */
    private void initialPool(){
        // 池基本配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(300);
        config.setMinIdle(10);
        config.setMaxTotal(500);
        config.setTestOnBorrow(true);
        config.setTimeBetweenEvictionRunsMillis(30000);
        config.setMinEvictableIdleTimeMillis(300000);
        config.setMaxWaitMillis(1000l);
        
        //測試環境

//        jedisPool = new JedisPool(config,"10.23.4.240",6379,300000,"09abade8-051a-48da-910d-4d9428377db4./beyondsoft!",2);
        //wuhan
        jedisPool = new JedisPool(config,"192.168.56.100",6379,300000,"09abade8-051a-48da-910d-4d9428377db4./beyondsoft!",2);

//        濟寧服務器
//        jedisPool = new JedisPool(config,"10.34.1.61",6379,30000,"09abade8-051a-48da-910d-4d9428377db4./beyondsoft!",2);
    }

    /**
     * 初始化切片池
     */
    private void initialShardedPool(){
        // 池基本配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(5);
        config.setMaxWaitMillis(1000l);
        config.setTestOnBorrow(false);
        // save链接
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        //測試服務器
//        shards.add(new JedisShardInfo("10.23.4.240", 6379, "master"));
        //濟寧服務
//        shards.add(new JedisShardInfo("10.34.1.61", 6379, "master"));
        //wuhan
        shards.add(new JedisShardInfo("192.168.56.100", 6379, "master"));
        // 构造池
        shardedJedisPool = new ShardedJedisPool(config, shards);
    }

    public void Close() {
       if(null != jedisPool){
    	   jedisPool.close();
       }
       if(null != shardedJedisPool){
    	   shardedJedisPool.close();
       }
    }
}
